package com.hz.ys_system.dao;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.hz.framework.MyAction.BaseAction;
import com.hz.framework.MyInterFace.SystemInterFace;
import com.hz.framework.dao.CommonDBDao;
import com.hz.framework.dao.CommonDao;
import com.hz.framework.util.DateUtil;
import com.hz.framework.util.SqlParameter;
import com.hz.ys_system.bean.PresMachineinfo;
import com.hz.ys_system.bean.PresUserinfo;
import com.hz.ys_system.dbsql.SystemSql;

/**
 * *****************************************************************************
 *               Copyright(c) 2010 by 哈尔滨华泽数码科技有限公司.
 *                       All rights reserved.
 *******************************************************************************
 *     File Name         :  com.hz.hzsearch.dao.DynamicDao.java
 *     Description(说明)	:  系统管理相关方法
 * -----------------------------------------------------------------------------
 *     No.        Date              Revised by           Description	  
 *     1		Jun 8, 2011				韩飞	              Created
 *****************************************************************************
 */
public class SystemDao implements SystemInterFace{
	
	/**
	 * @author 韩飞
	 * @time Nov 16, 2011	
	 * @return 
	 * 方法功能:查询下拉选名称信息
	 */
	public String SelSelect(HttpServletRequest request){
		
		return SystemSql.SEL_TYPE;
	}
	/**
	 * @author 韩飞
	 * @time Nov 16, 2011	
	 * @param request
	 * @throws Exception 
	 * 方法功能: 保存下拉选名称信息及对应的内容信息
	 */
	public void DoSaveSelectInfo(HttpServletRequest request) throws Exception {
		boolean ins = false;
		CommonDBDao dbd= new CommonDBDao();
		BaseAction ba = new BaseAction();
		String[] fieldt = {"typename","typechar","createuser","tid"};
		String[] fieldv = {"selectvalue","selectdsc","vid"};
		
		//	获得页面数据
		Map mapt = ba.dogetMap(request,fieldt); // 名称信息
		List listv = ba.dogetList(request,fieldv); //内容信息
		if(mapt!=null&&listv!=null){
			//保存或更新 下拉选名称表
			String[] fieldts = {"typename","typechar","createuser","createtime"};
			mapt.put("createtime", DateUtil.getDateTime());
			if("".equals(mapt.get("tid"))){
				ins = dbd.doInsert(mapt, fieldts, "pres_selecttype");
			}else{
				ins = dbd.doUpdate(mapt, fieldts, "pres_selecttype","tid");
			}
			
//			先删除内容信息，然后重新插入 下拉选内容信息表
			String[] fieldvs = {"selectvalue","selecttype","selectdsc","createuser","createtime"};
			dbd.DeleteSql("pres_selectvalue", "selecttype", "'"+mapt.get("typechar")+"'" );
			for(int i = 0;i<listv.size();i++){
				Map map = (Map)listv.get(i);
				map.put("createtime", DateUtil.getDateTime());
				map.put("selecttype", mapt.get("typechar"));
				map.put("createuser", mapt.get("createuser"));
				ins = dbd.doInsert(map, fieldvs, "pres_selectvalue");
			}
			request.setAttribute("msg", ins);
		}
	}
	
	/**
	 * @author 韩飞
	 * @time Nov 16, 2011	
	 * @param request
	 * @return 
	 * 方法功能: 删除 下拉选名称信息及 对应的内容信息
	 */
	public boolean Del_Select(HttpServletRequest request){
		String tid = CommonDao.isnull(request.getParameter("delid"));
		CommonDBDao dbo= new CommonDBDao();
		String[] sql = {"delete from pres_selectvalue where selecttype in (select typechar from pres_selecttype where tid ='"+tid+"')","delete from pres_selecttype where tid ='"+tid+"'"}; 
		return dbo.insorupdArray(sql);
	}
	
	/**
	 * @author 韩飞
	 * @time Nov 16, 2011	
	 * @param tid 根据 表中主键
	 * @return 
	 * 方法功能: 查询下拉选名称信息 及 对应的内容信息
	 */
	public void SelSelect_value(HttpServletRequest request){
		CommonDBDao dbo= new CommonDBDao();
		String tid = CommonDao.isnull(request.getParameter("tid"));
		
		request.setAttribute("typeinfo", dbo.queryListByPS(SystemSql.SEL_TYPEINFO, new SqlParameter(tid),null));
		request.setAttribute("valueinfo", dbo.queryListByPS(SystemSql.SEL_VALUEINFO, new SqlParameter(tid),null));
	}
	
	/**
	 * @author 韩飞
	 * @time Nov 16, 2011	
	 * @param typechar 根据 表中标识
	 * @return 
	 * 方法功能: 查询下拉选名称信息 及 对应的内容信息
	 */
	public static List SelSelect_Info(String typechar){
		CommonDBDao dbo= new CommonDBDao();
		return dbo.queryListByPS(SystemSql.SEL_VALUE, new SqlParameter(typechar),null);
	}
	
	
	/**
	 * @author 韩飞
	 * @time Nov 16, 2011	
	 * @return 
	 * 方法功能:查询用户信息
	 */
	public Map SelUserInfo(){
		CommonDBDao dbo= new CommonDBDao();
		return dbo.DBjsonMap(SystemSql.SEL_USERINFO);
	}
	/**
	 * @author 韩飞
	 * @time Nov 16, 2011	
	 * @return 
	 * 方法功能:查询 系统注册用户信息版本
	 */
	public List SelTimeInfo(){
		CommonDBDao dbo= new CommonDBDao();
		return dbo.DBjsonList(SystemSql.SEL_TIMEINFO);
	}
	/**
	 * @author 韩飞
	 * @time Nov 17, 2011	
	 * @param puser
	 * @return
	 * @throws Exception 
	 * 方法功能: 保存用户信息
	 */
	public boolean DoSaveUserInfo( PresUserinfo puser) throws Exception {
		boolean ins = false;
		CommonDao dbd= new CommonDao();
		if(puser!=null){
			//保存或更新 下拉选名称表
			dbd.insorupd("update pres_userinfo set selected = 'n' ");
			puser.setCreatetime(DateUtil.getDateTime());
			ins = dbd.insertinfor(puser);
		}
		return ins;
	}
	/**
	 * @author 韩飞
	 * @time Nov 16, 2011	
	 * @return 
	 * 方法功能:查询用户信息
	 */
	public boolean UpdUserInfo(String time){
		CommonDBDao dbo= new CommonDBDao();
		return dbo.insorupdArray(new String[]{"update pres_userinfo set selected = 'n'","update pres_userinfo set selected = 'y' where createtime = '"+time+"'"});
	}
	/**
	 * @author 韩飞
	 * @time Nov 16, 2011	
	 * @return 
	 * 方法功能:删除用户信息
	 */
	public boolean DelUserInfo(String time){
		CommonDBDao dbo= new CommonDBDao();
		if(dbo.insorupd("delete from pres_userinfo where createtime = '"+time+"'")){
			return dbo.insorupd("update pres_userinfo set selected = 'y' where id = (select max(id) from pres_userinfo )");
		}else{
			return false;
		}
	}
	
	/**
	 * @author 韩飞
	 * @time Nov 16, 2011	
	 * @return 
	 * 方法功能:查询机台信息
	 */
	public String SelSelMachineInfo(HttpServletRequest request){
		StringBuilder sbSQL = new StringBuilder();
		sbSQL.append(SystemSql.SEL_MACHINE_INFO);
		
		
		return sbSQL.toString();
	}
	
	/**
	 * @author 韩飞
	 * @time Nov 17, 2011	
	 * @param puser
	 * @return
	 * @throws Exception 
	 * 方法功能: 保存机台信息
	 */
	public boolean DoSaveMachineInfo( PresMachineinfo pm ) throws Exception {
		boolean ins = false;
		CommonDao dbd= new CommonDao();
		if(pm!=null){
			//保存或更新 下拉选名称表
//			mak.setGoodsid(CommonFunc.CreateRadom(11, 2));
//			pm.setCreatetime(DateUtil.getDateTime());
			ins = dbd.insertinfor(pm);
		}
		return ins;
	}
	/**
	 * @author 韩飞
	 * @time Nov 17, 2011	
	 * @param puser
	 * @return
	 * @throws Exception 
	 * 方法功能: 修改机台信息
	 */
	public boolean DoUpdMachineInfo( PresMachineinfo pm ) throws Exception {
		boolean ins = false;
		CommonDao dbd= new CommonDao();
		if(pm!=null){
			//保存或更新 下拉选名称表
			ins = dbd.updateinfor(pm);
		}
		return ins;
	}
	/**
	 * @author 韩飞
	 * @time Nov 16, 2011	
	 * @param request
	 * @return 
	 * 方法功能: 删除 机台信息
	 */
	public boolean DelMachineInfo(HttpServletRequest request){
		String mid = CommonDao.isnull(request.getParameter("mid"));
		CommonDBDao dbo= new CommonDBDao();
		return dbo.executeSQL(SystemSql.DEL_MACHINE, new SqlParameter(mid));
	}
	
	/**
	 * @author 韩飞
	 * @time Nov 16, 2011	
	 * @param request
	 * @return 
	 * 方法功能: 根据 id 查询 对应机台信息
	 */
	public Map SelMachineInfo(HttpServletRequest request) throws Exception {
		String mid = CommonDao.isnull(request.getParameter("mid"));
		CommonDao dbd= new CommonDao();
		return (Map)dbd.queryObjectByPS(SystemSql.SEL_MACHINE_ONE, new SqlParameter(mid),null);
	}
}
